<template>
  <el-dialog title="新增员工" :visible.sync="isShow" width="50%" @close="closeEvent">
    <!-- 表单 -->
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item prop="username" label="姓名">
        <el-input v-model="form.username" placeholder="1-30个字符" />
      </el-form-item>
      <el-form-item prop="mobile" label="手机">
        <el-input v-model="form.mobile" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item prop="timeOfEntry" label="入职时间">
        <el-date-picker v-model="form.timeOfEntry" type="date" placeholder="请选择时间" />
      </el-form-item>
      <el-form-item prop="formOfEmployment" label="聘用形式">
        <!-- <el-input v-model="form.username" placeholder="1-30个字符" /> -->
        <el-select v-model="form.formOfEmployment" placeholder="请选择">
          <el-option v-for="item in employeesData.hireType" :key="item.id" :label="item.value" value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item prop="workNumber" label="工号">
        <el-input v-model="form.workNumber" placeholder="1-30个字符" />
      </el-form-item>
      <el-form-item prop="departmentName" label="部门">
        <el-input v-model="form.departmentName" placeholder="1-30个字符" @focus="focusEvent" />
        <el-tree v-if="show" :data="listEmploy" :props="{ label: 'name' }" @node-click="nodeClick" />
      </el-form-item>
      <el-form-item prop="correctionTime" label="转正时间">
        <el-date-picker v-model="form.correctionTime" type="date" placeholder="请选择时间" />
      </el-form-item>
    </el-form>
    <!-- 结束表单 -->
    <!-- 按钮 -->
    <template #footer>
      <div style="text-align: center">
        <el-button type="primary" @click="btnClick">确定</el-button>
        <el-button @click="closeEvent">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
// 导入聘请形式
import employeesData from '@/api/constant/employees.js'
import { companyDepartment } from '@/api/departments.js'
// 新增员工
import { sysuserPost } from '@/api/employees.js'
// 导入手机号
import { checkPhone } from '@/utils/validate.js'
export default {
  data() {
    return {
      show: false,
      // js中不能直接使用import
      employeesData: employeesData,
      isShow: false,
      listEmploy: [],
      form: {
        username: '',	// string	非必须		姓名
        mobile: '', //	string	非必须		手机号
        formOfEmployment: '', //	number	非必须		聘用形式
        workNumber: '', //	string	非必须		工号
        departmentName: '', //	string	非必须		组织名称
        timeOfEntry: '', //	string	非必须		入职时间
        correctionTime: ''//	string	非必须		转正时
      },
      rules: {
        // 名字
        username: [{ required: true, message: '必填', trigger: 'blur' },
          { min: 1, max: 30, message: '长度在 3 到 5 个字符', trigger: 'blur' }],
        mobile: [{ required: true, message: '必填', trigger: 'blur' }, { validator: (rule, value, callback) => {
          checkPhone(value) ? callback() : callback(new Error('请输入正确的手机号'))
        } }],
        // 入职时间
        timeOfEntry: [{ required: true, message: '必填', trigger: 'blur' }],
        // 聘用
        formOfEmployment: [{ required: true, message: '必填', trigger: 'blur' }],
        // 工号
        workNumber: [{ required: true, message: '必填', trigger: 'blur' }, { min: 3, max: 5, message: '长度在 1 到 30 个字符', trigger: 'blur' }],
        departmentName: [{ required: true, message: '必填', trigger: 'blur' }],
        correctionTime: [{ required: true, message: '必填', trigger: 'blur' }]
      }
    }
  },
  methods: {
    async  focusEvent() {
      const res = await companyDepartment()
      console.log(res, 'ccc')
      this.listEmploy = this.changaData(res.data.depts, '')
      this.show = true
    },
    changaData(arr, str = '') {
      return arr.filter((item) => {
        if (item.pid === str) {
          item.children = this.changaData(arr, item.id)
          return true
        }
      })
    },
    nodeClick(row) {
      console.log(row)
      this.form.departmentName = row.name
      this.show = false
    },
    btnClick() {
      this.$refs.form.validate(async(val) => {
        if (val) {
          await sysuserPost(this.form)
          this.$message.success('新增成功')
          this.closeEvent()
          this.$emit('getData')
        }
      })
    },
    closeEvent() {
      this.isShow = false
      this.$refs.form.resetFields()
      this.form = {
        username: '',	// string	非必须		姓名
        mobile: '', //	string	非必须		手机号
        formOfEmployment: '', //	number	非必须		聘用形式
        workNumber: '', //	string	非必须		工号
        departmentName: '', //	string	非必须		组织名称
        timeOfEntry: '', //	string	非必须		入职时间
        correctionTime: ''//	string	非必须		转正时
      }
    }
  }
}
</script>

<style>

</style>
